// import { statisticPages_6409 } from '@/router/modules/bank_6409';
import { Commit, StoreOptions } from 'vuex';

const tagsView = {
    namespaced: true,
    state: {
        cachedViews: [],
        visitedViews: [], //   非业务菜单数据
        visitedViewsItem: [],// 业务菜单数据 tabs  里的数据
        cachedViewsItem: [],
    },
    mutations: {
        // 更新 visitedViews内容 和 cachedViews里的内容
        ADD_VISITED_VIEWS: (state: any, view: any) => {
            // 如果v.path === view.path 进入  return空

            if (state.visitedViews.some((v: any) => v.path === view.path)) return;
            // 更新 visitedViews 里的数据 从 view里
            if (!view.meta.noCache) {
                state.visitedViews.push(
                    Object.assign({}, view, {
                        title: view.meta.title || 'no-name'
                    })
                )
            }
            // 如果 noCache里没有值
            if (!view.meta.noCache) {
                // cachedViews更新
                state.cachedViews.push(view.name)
            }
        },
        //添加cache的组件名称 
        ADD_CACHED_VIEWS: (state: any, view: any) => {
            if (view.meta.noTageView && !view.meta.noCache) {
                let cacheIndex = state.cachedViews.indexOf(view.name);
                cacheIndex == -1 && state.cachedViews.push(view.name);
            }
        },

        // 更新 visitedViewsItem内容 和 cachedViewsItem 里的内容
        ADD_VISITED_VIEWS_ITEM: (state: any, view: any) => {
            // 如果v.path === view.path 进入  return空
            // console.log('state.cachedViewsItem', state.cachedViewsItem);
            if (state.visitedViewsItem.some((v: any) => v.path === view.path)) return

            // 更新 visitedViews 里的数据 从 view里
            state.visitedViewsItem.push(
                Object.assign({}, view, {
                    title: view.meta.title || 'no-name'
                })
            )
            // 如果 noCache里没有值
            if (!view.meta.noCache) {
                // cachedViews更新
                state.cachedViewsItem.push(view.name)
            }
        },
        // 删除缓存组件 nameValue 传需要删除的组件名称
        DELETE_CACHE_VIEWS: (state: any, nameValue: any) => {
            let cachedViews = JSON.parse(JSON.stringify(state.cachedViews))
            let cacheIndex = cachedViews.indexOf(nameValue);
            cachedViews.splice(cacheIndex, 1);
            // console.log(cachedViewsItem, '处理完成后的cachedViewsItem')
            state.cachedViews = cachedViews
        },

        // 删除缓存组件 nameList 传需要删除的组件名称数组
        DELETE_CACHE_VIEWS_ITEM: (state: any, nameList: any) => {
            let cachedViewsItem = JSON.parse(JSON.stringify(state.cachedViewsItem))
            nameList.forEach((item: any) => {
                cachedViewsItem = state.cachedViewsItem.filter((val: any) => item !== val)
            });

            // console.log(cachedViewsItem, '处理完成后的cachedViewsItem')
            state.cachedViewsItem = cachedViewsItem
        },


        // 删除同名的路由名称和路径
        DEL_VISITED_VIEWS: (state: any, view: any) => {
            // entries 把数组 转换成键值对的对象
            for (const [i, v] of state.visitedViews.entries()) {
                // 如果路径相等
                if (v.path === view.path) {
                    // 删除当前位置的路由名称
                    state.visitedViews.splice(i, 1)
                    break
                }
            }
            // 循环cachedViews里的值
            for (const i of state.cachedViews) {
                // 如果值相等
                if (i === view.name) {
                    // 保存在cachedViews里当前值的位置
                    const index = state.cachedViews.indexOf(i)
                    // 删除当前值
                    state.cachedViews.splice(index, 1)
                    break
                }
            }
        },
        // 删除 visitedViewsItem 里同名的路由名称和路径
        DEL_VISITED_VIEWS_ITEM: (state: any, view: any) => {
            // entries 把数组 转换成键值对的对象
            for (const [i, v] of state.visitedViewsItem.entries()) {
                // 如果路径相等
                if (v.path === view.path) {
                    // 删除当前位置的路由名称
                    state.visitedViewsItem.splice(i, 1)
                    break
                }
            }
            // 循环 cachedViewsItem 里的值
            for (const i of state.cachedViewsItem) {
                // 如果值相等
                if (i === view.name) {
                    // 保存在 cachedViewsItem 里当前值的位置
                    const index = state.cachedViewsItem.indexOf(i)
                    // 删除当前值
                    state.cachedViewsItem.splice(index, 1)
                    break
                }
            }
        },
        // 删除其他的views
        DEL_OTHERS_VISITED_VIEWS: (state: any, view: any) => {
            state.visitedViews = state.visitedViews.filter((v: any) => {
                return v.path === view.path;
            })
        },
        // 删除其他的cache
        DEL_OTHER_CACHED_VIEWS: (state: any, view: any) => {
            const index = state.cachedViews.indexOf(view.name);
            if (index > -1) {
                state.cachedViews = state.cachedViews.slice(index, index + 1);
            } else {
                // 如果 index = -1 代表存的不是 我要的 所以都可以删除
                state.cachedViews = []
            }
        },
        // 删除其他的views Item
        DEL_OTHERS_VISITED_VIEWS_ITEM: (state: any, view: any) => {
            state.visitedViewsItem = state.visitedViewsItem.filter((v: any) => {
                return v.meta.affix || v.path === view.path;
            })
        },
        // 删除其他的cache Item
        DEL_OTHER_CACHED_VIEWS_ITEM: (state: any, view: any) => {
            const index = state.cachedViewsItem.indexOf(view.name);
            if (index > -1) {
                state.cachedViewsItem = state.cachedViewsItem.slice(index, index + 1);
            } else {
                // 如果 index = -1 代表存的 不是我要的 所以都可以删除
                state.cachedViewsItem = []
            }
        },
        //删除 缓存里的路由和名称
        DEL_ALL_VISITED_VIEWS: (state: any) => {
            state.visitedViews = [];
        },
        DEL_ALL_CACHED_VIEWS: (state: any) => {
            state.cachedViews = [];
        },

        DEL_ALL_VISITED_VIEWS_ITEM: (state: any) => {
            state.visitedViewsItem = [];
        },
        DEL_ALL_CACHED_VIEWS_ITEM: (state: any) => {
            state.cachedViewsItem = [];
        },
        // 保留第一个业务视图
        DEL_NUM_CACHED_VIEWS_ITEM: (state: any) => {
            const cachedItemLeng = state.cachedViewsItem.length;
            state.cachedViewsItem.splice(1, cachedItemLeng);
        },
        // 保留第一个业务视图
        DEL_NUM_VISITED_VIEWS_ITEM: (state: any) => {
            const viewsItemLeng = state.visitedViewsItem.length;
            state.visitedViewsItem.splice(1, viewsItemLeng);
        },

    },
    actions: {
        // 添加缓存
        addVisitedViews: (context: { commit: Commit }, view: any) => {
            context.commit('ADD_VISITED_VIEWS', view)
        },
        addVisitedViewsItem: (context: { commit: Commit }, view: any) => {
            context.commit('ADD_VISITED_VIEWS_ITEM', view)
        },
        // ADD_CACHED_VIEWS 添加要缓存的组件名称
        addCachedViews: (context: { commit: Commit }, view: any) => {
            context.commit('ADD_CACHED_VIEWS', view)
        },
        // 删除同名缓存
        delVisitedViews: (context: { commit: Commit, state: any }, view: any) => {
            return new Promise(resolve => {
                context.commit('DEL_VISITED_VIEWS', view)
                resolve([...context.state.visitedViews])
            })
        },
        delVisitedViewsItem: (context: { commit: Commit, state: any }, view: any) => {
            return new Promise(resolve => {
                context.commit('DEL_VISITED_VIEWS_ITEM', view)
                resolve([...context.state.visitedViewsItem])
            })
        },
        // 其他
        delOthersVisitedViews(context: { commit: Commit, state: any }, view: any) {
            return new Promise(resolve => {
                context.commit('DEL_OTHERS_VISITED_VIEWS', view);
                resolve([...context.state.visitedViews]);
            })
        },
        delOthersCachedViews(context: { commit: Commit, state: any }, view: any) {
            return new Promise(resolve => {
                context.commit('DEL_OTHER_CACHED_VIEWS', view);
                resolve([...context.state.cachedViews]);
            })
        },
        // 其他 Item
        delOthersVisitedViewsItem(context: { commit: Commit, state: any }, view: any) {
            return new Promise(resolve => {
                context.commit('DEL_OTHERS_VISITED_VIEWS_ITEM', view);
                resolve([...context.state.visitedViews]);
            })
        },
        delOthersCachedViewsItem(context: { commit: Commit, state: any }, view: any) {
            return new Promise(resolve => {
                context.commit('DEL_OTHER_CACHED_VIEWS_ITEM', view);
                resolve([...context.state.cachedViews]);
            })
        },
        // 删除其他 tag
        delOtherViews(context: { dispatch: any, state: any }, view: any) {
            return new Promise(resolve => {
                context.dispatch('delOthersVisitedViews', view);
                context.dispatch('delOthersCachedViews', view);
                resolve({
                    visitedViews: [...context.state.visitedViews],
                    cachedViews: [...context.state.cachedViews],
                })
            })
        },
        delOtherViewsItem(context: { dispatch: any, state: any }, view: any) {
            return new Promise(resolve => {
                context.dispatch('delOthersVisitedViewsItem', view);
                context.dispatch('delOthersCachedViewsItem', view);
                resolve({
                    visitedViews: [...context.state.visitedViewsItem],
                    cachedViews: [...context.state.cachedViewsItem],
                })
            })
        },
        // 删除 所有
        delAllViews(context: { dispatch: any, state: any }, view: any) {
            return new Promise(resolve => {
                context.dispatch('delAllVisitedViewss', view);
                context.dispatch('delAllCachedViewss', view);
                resolve({
                    visitedViews: [...context.state.visitedViews],
                    cachedViews: [...context.state.cachedViews],
                })

            })
        },
        // 删除 所有
        delAllViewsItem(context: { dispatch: any, state: any }, view: any) {
            return new Promise(resolve => {
                context.dispatch('delAllVisitedViewssItem', view);
                context.dispatch('delAllCachedViewssItem', view);
                resolve({
                    visitedViews: [...context.state.visitedViewsItem],
                    cachedViews: [...context.state.cachedViewsItem],
                })

            })
        },
        // 删除 所有 Visited tag
        delAllVisitedViewss: (context: { commit: Commit, state: any }) => {
            return new Promise(resolve => {
                context.commit('DEL_ALL_VISITED_VIEWS');
                resolve([...context.state.visitedViews])
            })
        },
        // 删除 所有 cached tag
        delAllCachedViewss: (context: { commit: Commit, state: any }) => {
            return new Promise(resolve => {
                context.commit('DEL_ALL_CACHED_VIEWS');
                resolve([...context.state.cachedViews])
            })
        },
        // 删除 所有Item Visited  tag
        delAllVisitedViewssItem: (context: { commit: Commit, state: any }) => {
            return new Promise(resolve => {
                context.commit('DEL_NUM_VISITED_VIEWS_ITEM'); // 保留第一个业务视图
                resolve([...context.state.visitedViews])
            })
        },
        // 删除 所有Item cached  tag
        delAllCachedViewssItem: (context: { commit: Commit, state: any }) => {
            return new Promise(resolve => {
                context.commit('DEL_ALL_CACHED_VIEWS_ITEM');
                resolve([...context.state.cachedViews])
            })
        },

        // 删除所有缓存
        delAllVisitedViews: (context: { commit: Commit, state: any }) => {
            context.commit("DEL_ALL_VISITED_VIEWS");
            context.commit("DEL_ALL_CACHED_VIEWS");
        },

        delAllVisitedViewsItem: (context: { commit: Commit, state: any }) => {
            context.commit("DEL_ALL_VISITED_VIEWS_ITEM");
            context.commit("DEL_ALL_CACHED_VIEWS_ITEM");
        },
        // 删除 Visited 
        delAllVisitedOneViews: (context: { commit: Commit, state: any }) => {
            context.commit("DEL_ALL_VISITED_VIEWS");
        },
    },

}

export default tagsView;
